home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: newbie's problems w/ linux
- Date: Mon, 12 Feb 96 20:06:33 GMT
- Organization: none
- Message-ID: <824155593snz@genesis.demon.co.uk>
- References: <4fnrq8$53v@news.datatamers.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4fnrq8$53v@news.datatamers.com> wnc@datatamers.com "
- " writes:
-
- >i'm currently taking a beginners C class, and i was attempting to make a
- >program which figures out GPA for my classes. I'm running windows 95, and
- >when i compile it for DOS, it runs fine, but, when i compile it for
- >linux, using gcc, i keep getting this same error, basically what happens
- >is that after taking input through scanf(), it will skip the next
- >scanf(), even though i'm using fflush(). here is a sample output of what
- >happens.
- >
- >
- >GPA version .9beta, written by wnc@datatamers.com
- >
- >
- >How many classes do you have? 2
- >What grade did you get in period 1? a
- >What grade did you get in period 2? Do you have any AP classes?
- >
- >Your Total GPA for 2 period(s) is 0!
- >Which is equivilent to a(n) F
- >
- >
- >here is the source, if you can figure out what's wrong w/ it, please
- >mail me at wnc@datatamers.com, thanks in advance.
- >
- >#include <stdio.h>
- >void convert(void);
- >void endo(void);
- >float converted, gpa = 0., ap, donegpa;
- >int periods;
- >char grade, endofit[2], yesno;
- >void main()
-
- In C main returns an int.
-
- >{
- >short int counter, counter2;
-
- int is typically mapped to the most efficient type the archetecture
- can handle therefore you should use it for auxiliary variables unless
- you need something longer. Shorts have very little use other than in
- structures and arrays.
-
- >printf("\n\n\n\n\n\n\nGPA version .9beta, written by
- > wnc@datatamers.com\n\n\n");
- >printf("How many classes do you have? ");
- >scanf("%i", &periods);
-
- Look up the difference between the %i and %d scanf format specifiers. I'm
- not saying %i is necessarily wrong here (although it may be if you
- aren't using an ANSI compiler/library), just that %d more commonly
- corresponds to what was wanted.
-
- >fflush(stdin);
-
- This results in undefined behaviour. fflush() is only defined on output
- streams or update streams where the last operation was a write. You can
- also pass NULL to fflush (many pre-ANSI libraries didn't support this
- though.
-
- >for ( counter = 1; counter <= periods; ++counter )
- >{
- >printf("What grade did you get in period %i? ", counter);
-
- %d and %i means the same thing in printf formats. However %d has been
- supported much longer by C compilers/libraries and is more commonly used.
-
- Here is where you may need a fflush(). The C runtime system may have set
- stdout as line buffered here in which case it won't write anything until
- the program writes a newline to stdout. Alternatively you can force buffered
- output to be written using:
-
- fflush(stdout);
-
- >scanf("%d", &grade);
-
- Here you are passing a pointer to a char to a format specifier which
- requires a pointer to an int. GCC should have warned you about this
- if you used an adequate warning level (-Wall will probably do it).
-
- >fflush(stdin);
- >convert();
- >gpa += converted;
- >}
- >printf("Do you have any AP classes? ");
- >scanf("%d",& yesno);
-
- Again you are trying to read an int value into a char and passing the
- wring type of pointer which is illegal. It looks from below that you were
- trying to read a charater in which case you could use:
-
- scanf(" %c",& yesno);
-
- Note the space is important since it skips over leading white-space in the
- input data (which %c doesn't do automatically).
-
- A generally better approach for getting input is to read in whole lines
- using fgets() into a buffer and then scanning them with sscanf, atoi,
- strtok etc. scanf by itself isn't too hot at dealing with line based
- input.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-